package nl.saxion.act.main_menu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import nl.saxion.act.playground.Prefs;
import nl.saxion.act.playground.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

public class HighscoresActivity extends Activity{
	
	String[] names = {"Joost", "Pieter", "Joost", "Hans", "Fred"};
	String[] scores = {"50", "40", "30", "20", "10"};

	private Prefs prefs = new Prefs(this);
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.highscores);
		
		for(int k = 1; k < 6; k++) {
			if(prefs.getPreference("name" + k) == "") {
				prefs.setPreference("name" + k, names[k-1]);
				prefs.setPreference("score" + k, scores[k-1]);
			} 
		}
		
		int score1 = (int)getIntent().getIntExtra("score1", -1);
		int score2 = (int)getIntent().getIntExtra("score2", -1);
		String name1 = (String)getIntent().getStringExtra("name1");
		String name2 = (String)getIntent().getStringExtra("name2");
		if(score1 > 0) {
			addScore(name1, score1);
		}
		if(score2 > 0) {
			addScore(name2, score2);
		}
		
		updateScores();
	}
	
	public void addScore(String name, int score) {
		for(int i = 1; i < 6; i++) {
			if(Integer.parseInt(prefs.getPreference("score" + i)) < score) {
				for(int j = 5; j > i; j--) {
					prefs.setPreference("score" + j, prefs.getPreference("score" + (j-1)));
					prefs.setPreference("name" + j, prefs.getPreference("name" + (j-1)));
				}
				prefs.setPreference("score" + i, Integer.toString(score));
				prefs.setPreference("name" + i, name);
				break;
			}
		}
	}
	
	public void updateScores() {
		TextView textView1 = (TextView) findViewById(R.id.text_highscores1);
		textView1.setText(prefs.getPreference("name1") + ": " + prefs.getPreference("score1"));
		TextView textView2 = (TextView) findViewById(R.id.text_highscores2);
		textView2.setText(prefs.getPreference("name2") + ": " + prefs.getPreference("score2"));
		TextView textView3 = (TextView) findViewById(R.id.text_highscores3);
		textView3.setText(prefs.getPreference("name3") + ": " + prefs.getPreference("score3"));
		TextView textView4 = (TextView) findViewById(R.id.text_highscores4);
		textView4.setText(prefs.getPreference("name4") + ": " + prefs.getPreference("score4"));
		TextView textView5 = (TextView) findViewById(R.id.text_highscores5);
		textView5.setText(prefs.getPreference("name5") + ": " + prefs.getPreference("score5"));
	}
	
	public void resetScores() {
		prefs.clearAllPreferences();
		updateScores();
	}
	
	public String readRawTextFile(Context ctx, int resId) {
         InputStream inputStream = ctx.getResources().openRawResource(resId);

            InputStreamReader inputreader = new InputStreamReader(inputStream);
            BufferedReader buffreader = new BufferedReader(inputreader);
             String line;
             StringBuilder text = new StringBuilder();

             try {
               while (( line = buffreader.readLine()) != null) {
                   text.append(line);
                   text.append('\n');
                 }
           } catch (IOException e) {
               return null;
           }
             return text.toString();
    }
 }
